package com.cyn.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cyn.aframework.http.HttpUtils;
import com.cyn.aframework.jd.JsoupHtmlParseJd;
import com.cyn.aframework.jd.ProductJson;
import com.cyn.entity.Project;
import com.cyn.service.ProjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("project")
public class ProjectController {


    @Autowired
    private ProjectService projectService;

    @GetMapping("insertProject")
    public String insert(String kewords,String pageNo) {
        try {
            List<ProductJson> productJsonList = JsoupHtmlParseJd.parseJD(kewords,pageNo);
            for (ProductJson p:productJsonList) {
                Project project = new Project();
                BeanUtils.copyProperties(p, project);
                if(!"".equals(project.getTitle())) {
                    project.setPageNo(pageNo);
                    Project project1 = projectService.insert(project);
                }

            }
        } catch (IOException e) {
            return "0";
        }
        return "1";
    }

    @GetMapping("insertImg")
    public String insertImg(String kewords,String pageNo) {
        try {
//            List<ProductJson> productJsonList = JsoupHtmlParseJd.parseJD(kewords,pageNo);

            String url = "https://www.mxnzp.com/api/image/girl/list/random";
            String s = HttpUtils.sendGet(url,"app_id=j1mbdkicigcxikli&app_secret=R2xaUGYxajZKZWZWLzlSNDJnZTFldz09");
            JSONObject jsonObject = JSONObject.parseObject(s);
            System.out.println(jsonObject.get("data"));
            JSONArray jsonArray = jsonObject.getJSONArray("data");
            jsonArray.stream().forEach(object-> {

                Project project = new Project();
                JSONObject jsonObject1 = (JSONObject) object;
                project.setImg(jsonObject1.getString("imageUrl"));
                project.setPageNo(pageNo);
                Project project1 = projectService.insert(project);
            });
//            for (ProductJson p:productJsonList) {
//                Project project = new Project();
//                BeanUtils.copyProperties(p, project);
//                if(!"".equals(project.getTitle())) {
//                    project.setPageNo(pageNo);
//                    Project project1 = projectService.insert(project);
//                    System.out.println(project1.toString());
//                }
//
//            }
        } catch (Exception e) {
            return "0";
        }
        return "1";
    }

    @GetMapping("getList")
    public Map<String,Object> getList(){
        Long begin = System.currentTimeMillis();
        List<Project> projectList = projectService.getList();
        Map<String,Object> params = new HashMap<>();
        params.put("data",projectList);
        params.put("count",projectList.size());
        params.put("date",System.currentTimeMillis()-begin);
        return params;
    }

    @GetMapping("getListCount")
    public Map<String,Object> getListCount(){
        Long begin = System.currentTimeMillis();
        Integer count = projectService.getListCount();
        Map<String,Object> params = new HashMap<>();
        params.put("count",count);
        params.put("date",System.currentTimeMillis()-begin);
        return params;
    }

}
